import 'package:flutter/material.dart';


class SecondPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SecondPageState();
  }
}

class SecondPageState extends State<SecondPage> {

  ///代码清单2-4
  ///lib/code2/second_page.dart
  String ? _title;
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();

    ModalRoute? route=ModalRoute.of(context);
    if(route!=null){
      //是否是路由栈中的第一个页面
      bool? isFirst = route.isFirst;
      //当前手机屏幕上显示的是否是这个页面Widget
      bool? isCurrent = route.isCurrent;
      //当前Widget是否是活跃可用的
      //当调用 pop 或者是关闭当前Widget时 isActive 为false
      bool isActive = route.isActive;
      if (isActive) {
        //获取路由信息
        RouteSettings routeSettings = route.settings;
        //获取传递的参数
        Object? arguments = routeSettings.arguments;
        if(arguments!=null&&arguments is Map<String, String>){

          Map<String, String> map = arguments;
          String? title = map["name"];
          print(
              "接收到参数 $title isCurrent$isCurrent isFirst$isFirst  isActive$isActive");
          //变量赋值
          _title = title;
        }

      }
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.red,
      appBar: AppBar(
        title: const Text("B页面页面"),
      ),
      body: Center(
        child: ElevatedButton(
            onPressed: () {
              Navigator.of(context).maybePop();
            },
            child: const Text("关闭当前页面")),
      ),
    );
  }
  //关闭B页面
  void closeBFunction() {
    //这里是向上一个页面回传的数据
    Map<String, String> resultMap = new Map();
    resultMap["name"] = "李四";

    ///回传数据
    Navigator.of(context).pop(resultMap);
  }

}
